package com.example.lexiangbackend.repository;

import com.example.lexiangbackend.entity.Music;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MusicRepository extends JpaRepository<Music, Long> {
    
    /**
     * 根据歌曲标题搜索（模糊查询）
     */
    Page<Music> findByTitleContainingIgnoreCase(String title, Pageable pageable);
    
    /**
     * 根据艺术家搜索（模糊查询）
     */
    Page<Music> findByArtistContainingIgnoreCase(String artist, Pageable pageable);
    
    /**
     * 综合搜索（歌曲标题、艺术家）
     */
    @Query("SELECT m FROM Music m WHERE " +
           "LOWER(m.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
           "LOWER(m.artist) LIKE LOWER(CONCAT('%', :keyword, '%'))")
    Page<Music> searchByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 根据播放次数排序获取热门音乐
     */
    Page<Music> findAllByOrderByPlayCountDesc(Pageable pageable);
    
    /**
     * 根据上传时间排序获取最新音乐
     */
    Page<Music> findAllByOrderByUploadTimeDesc(Pageable pageable);
    
    /**
     * 根据分类ID查找音乐
     */
    @Query("SELECT m FROM Music m WHERE m.category.categoryId = :categoryId")
    Page<Music> findByCategoryId(@Param("categoryId") Long categoryId, Pageable pageable);
    
    /**
     * 获取前N条音乐用于随机推荐
     */
    Page<Music> findAllByOrderByMusicIdAsc(Pageable pageable);
}